package mobi.yuanshen.common.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import mobi.yuanshen.common.constants.ContexConstance;
import mobi.yuanshen.common.domain.dto.PicCodeDTO;
import mobi.yuanshen.common.service.CommonService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@RequiredArgsConstructor
public class CommonServiceImpl implements CommonService {

    private final StringRedisTemplate redisTemplate;

    @Override
    public PicCodeDTO getPicCode() {
        String picKey = IdUtil.simpleUUID();
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 50);
        redisTemplate.opsForValue().set(
                ContexConstance.REDIS_KEY_PIC_CODE + picKey,
                captcha.getCode(),
                ContexConstance.REDIS_KEY_PIC_CODE_SEC,
                TimeUnit.SECONDS);
        log.info("生成验证码: {},{}", captcha.getCode(),picKey);
        return PicCodeDTO
                .builder()
                .picCode(captcha.getImageBase64Data())
                .picKey(picKey)
                .build();
    }
}
